//
//  MBProgressHUD+KL.m
//  ACE
//
//  Created by Mickey Yin on 2021/12/6.
//

#import "MBProgressHUD+KL.h"

#define kSleepTime 1.0
#define kScaleNum 1.1

@implementation MBProgressHUD (KL)

#pragma mark 没有文字
+ (void)showLoadingInView:(UIView *)view animated:(BOOL)animated {
    if (!view) return;
    MBProgressHUD *hud =[self MBProgressHUDWithView:view];
    [view addSubview:hud];
    [self projectStyleWithHud:hud];
    [hud showAnimated:YES];
}

#pragma mark 有文字
+ (void)showLoadingInView:(UIView *)view text:(NSString *)text animated:(BOOL)animated{
    if (!view ||
        [text isKindOfClass:[NSNull class]] ||
        !text.length) return;
    
    MBProgressHUD *hud = [self MBProgressHUDWithView:view];
    hud.label.text = text;
    [view addSubview:hud];
    [self projectStyleWithHud:hud];
    hud.label.font = [UIFont systemFontOfSize:kAutoFont(14)];
    hud.label.textColor = [UIColor whiteColor];
    [hud showAnimated:YES];
}

#pragma mark 纯文字提示
+ (void)showTextInView:(UIView *)view text:(NSString *)text animated:(BOOL)animated{
    if (!view) return;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:animated];
    hud.mode = MBProgressHUDModeText;
    [self projectStyleWithHud:hud];
    if (text == nil || [text isKindOfClass:[NSNull class]]) {
        hud.label.text = @"";
    }else{
        hud.label.text = text;
    }
    hud.margin = 15.f;
    hud.removeFromSuperViewOnHide = animated;
    [hud hideAnimated:animated afterDelay:kSleepTime];
}

+ (MBProgressHUD *)MBProgressHUDWithView:(UIView *)view {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.removeFromSuperViewOnHide = YES;
    return hud;
}

+ (void)projectStyleWithHud:(MBProgressHUD *)hud {
    hud.backgroundColor = [UIColor clearColor];
    hud.bezelView.layer.cornerRadius = 5.f;
    [hud.bezelView setBackgroundColor:rgba(0, 0, 0, 1)];
    hud.contentColor = [UIColor whiteColor];
    hud.label.font = [UIFont systemFontOfSize:12.f];
    hud.label.numberOfLines = 0;
    hud.detailsLabel.font = [UIFont systemFontOfSize:12.f];
    if (hud.mode != MBProgressHUDModeText) {
        CGRect labelFrame = hud.label.frame;
        labelFrame.origin.y += 20;
        hud.label.frame = labelFrame;
    }
}

#pragma mark 移除提示
+ (void)hideHudInView:(UIView *)view {
    for (UIView *child in view.subviews) {
        if ([child isKindOfClass:[MBProgressHUD class]]) {
            [child removeFromSuperview];
        }
    }
}

@end
